package com.df.excel_export.validator;

import lombok.extern.slf4j.Slf4j;

import java.util.*;

@Slf4j
public class ValidatorManager {

//    //容器 装所有校验器
//    public static List<IFieldValidator> validators  = new ArrayList<>();
//    //{校验器名：校验器实例}
//    public static Map<String,IFieldValidator> validatorsMap  = new HashMap<>();
    //数组类型规则的索引解析器
    public static List<IArrayIndexParser> indexParsers  = new ArrayList<>();

    //校验器工厂
    public static List<IValidatorFactory> validatorFactories  = new ArrayList<>();

    static {
        //测试环境下使用
//        validators.add(new DbListValidatorI());
//        validators.add(new DefaultValidatorI());
//        validators.add(new EnumValidatorI());
//        validators.add(new IdValidatorI());
//        validators.add(new LenValidatorI());
//        validators.add(new ListValidatorI());
//        validators.add(new RangeValidatorI());
    }

    public static void registerValidatorFactory(List<IValidatorFactory> factories) {
        validatorFactories.addAll(factories);
    }

    public static IFieldValidator parseValidator(String fieldTypeStr, String fieldRules) {
        List<IValidatorFactory> factories = new ArrayList<>();
        for (IValidatorFactory validator : validatorFactories) {
            if(validator.support(fieldTypeStr, fieldRules)){
                factories.add(validator);
                return validator.create(fieldTypeStr, fieldRules);
            }
        }
        if(factories.isEmpty()){
            return null;
        }
        if(factories.size()>1){
            factories.sort((a,b)->b.getPriority()-a.getPriority());
        }

        //TODO 检查脚本校验器
        return factories.get(0).create(fieldTypeStr,fieldRules);
    }
    public static List<Integer> parseElementIndex(String indexStr) {
        for (IArrayIndexParser parser : indexParsers) {
            if(parser.support(indexStr)){
                return parser.parseIndex(indexStr);
            }
        }
        return new ArrayList<>();
    }

    //TODO 加载脚本校验器

    //TODO 收集预定义的校验器

}
